#include "drv_ed_utility.h"
#include "shell.h"
#include "drv_led.h"

static EvtState PrintCntEvt(Event * const evt)
{
    evt->exeState++;
    printf("kpevt-sn = %lu, cnt = %lu\n", evt->evtSN, evt->exeState);
    return ED_TRAN(EVT_HANDLED);
}

static EvtState WaitEvt(Event * const evt)
{
    evt->exeState++;
    printf("cpevt-sn = %lu, cnt = %lu\n", evt->evtSN, evt->exeState);
    return ED_TRAN(EVT_HANDLED);
}

static uint32_t g_cntSN = 0;
static uint32_t g_waitSN = 0;
void ED4E_TestInit(void)
{
    if (g_cntSN == 0) {
        g_cntSN = ED4E_PublishKeyPeriodicEvent(PrintCntEvt, 1000);
    }
    if (g_waitSN == 0) {
        g_waitSN = ED4E_PublishPeriodicEvent(WaitEvt, 1000);
    }
}

static EvtState UrgentEvt(Event * const evt)
{
    printf("urgent event, sn = %lu\n", evt->evtSN);
    return ED_TRAN(EVT_HANDLED);
}

static void ShellCmd_ED4E_Test(const ShellCmdArgs *args)
{
    const char *arg = args->getArg(0);
    if (arg == NULL) {
        ED4E_TestInit();
        return;
    }
    switch (*arg) {
    case '0':
        ED4E_SetKeyPeriodicEvent(g_cntSN, EVT_PERIOD_PAUSE);
        break;
    case '1':
        ED4E_SetKeyPeriodicEvent(g_cntSN, EVT_PERIOD_RESUME);
        break;
    case '2': {
        ED4E_SetKeyPeriodicEvent(g_cntSN, EVT_KILL);
        g_cntSN = 0;
        }
        break;
    case '3':
        ED4E_SetPeriodicEvent(g_waitSN, EVT_PERIOD_PAUSE);
        break;
    case '4':
        ED4E_SetPeriodicEvent(g_waitSN, EVT_PERIOD_RESUME);
        break;
    case '5': {
        ED4E_SetPeriodicEvent(g_waitSN, EVT_KILL);
        g_waitSN = 0;
        }
        break;
    case '6':
        ED4E_PublishUrgentEvent(UrgentEvt);
        break;
    default:
        break;
    }
}
EXPORT_SHELL_CMD("ed", ShellCmd_ED4E_Test);
